/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.event.KeyEvent;
import mx.edu.itschapala.sistemas.septimo.control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVentana;

/**
 *
 * @author Estudiante
 */
public class VisCliente extends javax.swing.JFrame {
    
    private ClienteLnLocal clienteControl;
    private TipoVentana tipoVentana;
    private Cliente cliente;
    /**
     * Creates new form VisCliente
     */
     public VisCliente(Cliente cliente,ClienteLnLocal clienteControl, TipoVentana tipoVentana) {
       
        this.tipoVentana = tipoVentana;
        this.clienteControl= clienteControl;
     initComponents();
        switch(tipoVentana){
            case VENTANA_REGISTRO:
                  jLabel1.setText("Ingresar nuevo cliente");
                break;
            case VENTANA_MODIFICAR:
                 jLabel1.setText("Modificar Id cliente"); 
                break;
            case VENTANA_BUSQUEDA:
                 jLabel1.setText("BUSCAR POR ID");
                 jButton1.setText("Buscar");
                 jTextField1.setEditable(false);
                 jTextField3.setEditable(false);
                 jTextField4.setEditable(false);
                 jTextField5.setEditable(false);
                 break;
            case VENTANA_ELIMINAR:
                jLabel1.setText("Eliminar Cliente");
                jButton2.setText("Eliminar");
                break;
        }
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Times New Roman", 3, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(240, 240, 240));
        jLabel1.setText("CLIENTE");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 0, -1, -1));

        jLabel2.setForeground(new java.awt.Color(240, 240, 240));
        jLabel2.setText("NOMBRE");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 40, -1, -1));

        jLabel3.setForeground(new java.awt.Color(240, 240, 240));
        jLabel3.setText("ID");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 80, -1, -1));

        jLabel4.setForeground(new java.awt.Color(240, 240, 240));
        jLabel4.setText("TELEFONO");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 120, -1, -1));

        jLabel5.setForeground(new java.awt.Color(240, 240, 240));
        jLabel5.setText("DIRECCION");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 170, -1, -1));

        jLabel6.setForeground(new java.awt.Color(240, 240, 240));
        jLabel6.setText("SEXO");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 210, -1, -1));

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });
        jPanel1.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 40, 160, -1));
        jPanel1.add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 80, 160, -1));
        jPanel1.add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 120, 160, -1));
        jPanel1.add(jTextField4, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 160, 160, -1));
        jPanel1.add(jTextField5, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 200, 160, -1));

        jButton1.setBackground(new java.awt.Color(0, 0, 0));
        jButton1.setText("ACEPTAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 300, -1, -1));

        jButton2.setText("CANCELAR");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 300, -1, -1));

        jLabel7.setIcon(new javax.swing.ImageIcon("C:\\Users\\Estudiante\\Desktop\\Packet tracer 6.0.1\\maverick 02.jpg")); // NOI18N
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 600, 440));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
          this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
          switch (tipoVentana) {
            case VENTANA_REGISTRO:
                Cliente c = new Cliente();
                
                c.setId(Integer.parseInt(jTextField2.getText()));
                c.setNombre(jTextField1.getText());
                c.setTelefono(Integer.parseInt(jTextField3.getText()));
                c.setDireccion(jTextField4.getText());
                c.setSexo(jTextField5.getText());
                clienteControl.registrar(c);
                jTextField2.setText("");
                jTextField1.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                break;
            case VENTANA_MODIFICAR:
                this.dispose();
                break;
            case VENTANA_BUSQUEDA:
                c = new Cliente();
                c.setNombre(jTextField1.getText());
                c.setId(Integer.parseInt(jTextField2.getText()));
                c.setTelefono(Integer.parseInt(jTextField3.getText()));
                c.setDireccion(jTextField4.getText());
                c.setSexo(jTextField5.getText());
                clienteControl.modificar(c);
                jTextField2.setText("");
                jTextField1.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                jTextField1.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                break;
            case VENTANA_ELIMINAR:
                c = new Cliente();
                c.setId(Integer.parseInt(jTextField2.getText()));
                clienteControl.eliminar(c);
                
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
          if(tipoVentana!=TipoVentana.VENTANA_REGISTRO){
            if(evt.getKeyCode()==KeyEvent.VK_ENTER){
                
         Cliente c= clienteControl.buscarPorId(Integer.parseInt(jTextField2.getText()));
         if(c!=null){
             jTextField1.setText(c.getNombre());
             jTextField3.setText(c.getTelefono()+"");
             jTextField4.setText(c.getDireccion());
             jTextField5.setText(c.getSexo());
             if(tipoVentana==TipoVentana.VENTANA_MODIFICAR){
                jTextField1.setEditable(true);
                jTextField3.setEditable(true);
                jTextField4.setEditable(true);
                jTextField5.setEditable(true);
             }
         }
                
            }
        }
    }//GEN-LAST:event_jTextField1KeyPressed

    
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
